// pages/print/puzzle/puzzle.js
var utils = require("../../../utils/utils.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageUrls: [],
    cIndex: ''
  },
  chooseIndex(e) {
    var that = this;
    var tag = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    this.setData({
      cIndex: tag
    })
    switch (tag) {
      case 'reductionTypesetting_A4':   //a4拼版
        this.chooseStyle('A4', 'A4拼版', tag, id)
        return false;
      case 'reductionTypesetting_P6':    //6寸拼版
        this.chooseStyle('6', '6寸拼版', tag, id)
        return false;
      case 'reductionTypesetting_documentReduction':    //文档缩印
        wx.chooseMessageFile({
          count: 1,
          type: 'file',
          extension: ['doc', 'xls', 'ppt', 'docx', 'xlsx', 'pptx', 'pdf'],
          success(res) {
            var reg = /^.*\.(?:xls|xlsx)$/i;
            if (reg.test(res.tempFiles[0].name)) {
              //excel
              that.excelTpdf(res.tempFiles[0].path, res.tempFiles[0].name, tag, id)
            } else {
              //非excel
              that.settingDoc(res.tempFiles[0].path, res.tempFiles[0].name, tag, id)
            }
          }
        })
        return false;
    }
  },
  getIndex() {
    var that = this;
    utils.request('serviceItem/serviceItemLists', {}, function (res) {
      if (res.data.code == 200) {
        var docArray = [];
        for (var i = 0; i < res.data.data.length; i++) {
          if (that.data.tagFloor == res.data.data[i].config.tag) {
            docArray = res.data.data[i].child
          }
        }
        that.setData({
          list: docArray
        })
      } else {
        utils.toast(res.data.msg)
      }
    })
  },
  //文档处理
  settingDoc(path, name, code, id) {
    var that = this;
    that.selectComponent("#loading").showLoadingCus();
    utils.uploadfile(path, name, '', '', '', function (res) {
      var data = JSON.parse(res.data).data
      that.selectComponent("#loading").hideLoadingCus();
      wx.navigateTo({
        url: '/pages/print/printer/printer?code=' + code + '&name=文档缩印&path=' + data.printUrl + '&pageNum=' + data.pageTotal + '&fileUrl=' + data.fileUrl + '&serviceMainId=' + that.data.serviceMainId + '&serviceSubtypeId=' + id + '&tagFloor=' + that.data.tagFloor
      })
    })
  },
  //excel转pdf
  excelTpdf(path, name, code, id) {
    var that = this;
    that.selectComponent("#loading").showLoadingCus();
    utils.uploadfileExcel(path, name, function (res) {
      var data = JSON.parse(res.data).data
      that.selectComponent("#loading").hideLoadingCus();
      if (data.length == 1) {
        wx.showActionSheet({
          alertText: '请选择打印横竖版',
          itemList: ['竖版', '横版'],
          success(res) {
            if (res.tapIndex == 0) {
              that.selectComponent("#loading").showLoadingCus();
              utils.uploadfile('', data[0].sheetName, data[0].downUrl, '', '1', function (res) {
                var data = res.data.data
                that.selectComponent("#loading").hideLoadingCus();
                wx.navigateTo({
                  url: '/pages/print/printer/printer?code=' + code + '&name=文档缩印&path=' + data.printUrl + '&pageNum=' + data.pageTotal + '&fileUrl=' + data.fileUrl + '&serviceMainId=' + that.data.serviceMainId + '&serviceSubtypeId=' + id + '&tagFloor=' + that.data.tagFloor
                })
              })
            }
            if (res.tapIndex == 1) {
              that.selectComponent("#loading").showLoadingCus();
              utils.uploadfile('', data[0].sheetName, data[0].downUrl, '', '2', function (res) {
                var data = res.data.data
                that.selectComponent("#loading").hideLoadingCus();
                wx.navigateTo({
                  url: '/pages/print/printer/printer?code=' + code + '&name=文档缩印&path=' + data.printUrl + '&pageNum=' + data.pageTotal + '&fileUrl=' + data.fileUrl + '&serviceMainId=' + that.data.serviceMainId + '&serviceSubtypeId=' + id + '&tagFloor=' + that.data.tagFloor
                })
              })
            }
          },
        })
      } else {
        wx.navigateTo({
          url: '/pages/print/chooseSheet/chooseSheet?list=' + JSON.stringify(data) + '&serviceMainId=' + that.data.serviceMainId + '&serviceSubtypeId=' + id + '&tagFloor=' + that.data.tagFloor + '&code=' + code + '&isCY=1',
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      tagFloor: options.code,
      serviceMainId: options.serviceMainId
    })
    this.getIndex()
  },
  //选择照片方式
  chooseStyle(paperType, typeName, tag, id) {
    var that = this;
    that.setData({
      imageUrls: []
    })
    wx.showActionSheet({
      itemList: ['相册', '拍照', '从QQ会话选择'],
      success(res) {
        if (res.tapIndex == 0) {
          that.getPhoto('album', paperType, typeName, tag, id);
        }
        if (res.tapIndex == 1) {
          that.getPhoto('camera', paperType, typeName, tag, id);
        }
        if (res.tapIndex == 2) {
          that.getPhoto1(paperType, typeName, tag, id);
        }
      },
      fail(res) {

      }
    })
  },
  getPhoto1(paperType, typeName, tag, id) {
    wx.chooseMessageFile({
      count: 9,
      type: 'image',
      success: (res) => {
        this.setData({ imageUrls: [...this.data.imageUrls, ...res.tempFiles.map(f => f.path)] })
        var that = this;
        wx.navigateTo({
          url: '/pages/print/puzzles/puzzles?imgArray=' + JSON.stringify(this.data.imageUrls) + '&code=' + tag + '&sizeName=' + paperType + '&typeName=' + typeName + '&serviceSubtypeId=' + id + '&serviceMainId=' + that.data.serviceMainId + '&tagFloor=' + that.data.tagFloor,
        })
        // var array = [];
        // for (var i = 0; i < res.tempFiles.length; i++) {
        //     array.push(res.tempFiles[i].path)
        // }
        // that.setData({
        //     chooseImageArray: array
        // })
        // //轮巡判断上传
        // that.uploadFileImg(that.data.chooseImageArray[0], 0);
      }
    })
  },
  getPhoto(type, paperType, typeName, tag, id) {
    if (getApp().isLowerThenVersion("2.10.0")) {
      wx.chooseImage({
        count: this.data.maxUploadImageCount - this.data.imageUrls.length,
        sizeType: ['original'],
        sourceType: [type],
        success: (res) => {
          this.setData({ imageUrls: [...this.data.imageUrls, ...res.tempFiles.map(f => f.path)] })
          var that = this;
          wx.navigateTo({
            url: '/pages/print/puzzles/puzzles?imgArray=' + JSON.stringify(this.data.imageUrls) + '&code=' + tag + '&sizeName=' + paperType + '&typeName=' + typeName + '&serviceSubtypeId=' + id + '&serviceMainId=' + that.data.serviceMainId + '&tagFloor=' + that.data.tagFloor,
          })
        }
      })
    }
    else {
      wx.chooseMedia({
        count: this.data.maxUploadImageCount - this.data.imageUrls.length,
        mediaType: ['image'],
        sourceType: [type],
        sizeType: ['original'],
        success: (res) => {
          this.setData({ imageUrls: [...this.data.imageUrls, ...res.tempFiles.map(f => f.tempFilePath)] })
          var that = this;
          wx.navigateTo({
            url: '/pages/print/puzzles/puzzles?imgArray=' + JSON.stringify(this.data.imageUrls) + '&code=' + tag + '&sizeName=' + paperType + '&typeName=' + typeName + '&serviceSubtypeId=' + id + '&serviceMainId=' + that.data.serviceMainId + '&tagFloor=' + that.data.tagFloor,
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
})